package com.njust.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njust.bean.City;
import com.njust.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
public class CityController {

    @Autowired
    private CityService cityService;


    @RequestMapping("/cities")
    public String cityPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model) {

        //紧跟他的查询就是一个分页查询
        PageHelper.startPage(pageNum, 10);
        List<City> list = cityService.getCityAll();
        System.err.println(list.size());
        //我们可以将查询的结果使用；将查询的结果放在pageinfo中这个pageInfo就有非常多能够用的
        //第二份传入连续要显示的页码
        PageInfo<City> pageInfo = new PageInfo<>(list);
        System.err.println("当前页码：" + pageInfo.getPageNum());
        System.err.println("总页码：" + pageInfo.getPages());
        System.err.println("总记录数：" + pageInfo.getTotal());
        System.err.println("当前页有几条记录：" + pageInfo.getSize());
        System.err.println("当前页的pageSize：" + pageInfo.getPageSize());
        System.err.println("前一页：" + pageInfo.getPrePage());
        System.err.println("结果：" + pageInfo.getList());
        //查询结果
        int[] nums = pageInfo.getNavigatepageNums();
        System.err.println(Arrays.toString(nums));

        model.addAttribute("pageInfo", pageInfo);

        return "city.jsp";

    }
}
